package com.example.zk2cp;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

//不适用SP的工具类
public class MainActivity extends AppCompatActivity {

    private EditText etUsername;
    private EditText etPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        requestPermissions(new String[]{
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.READ_CONTACTS,
                Manifest.permission.WRITE_CONTACTS,
                Manifest.permission.READ_SMS,
                Manifest.permission.CALL_PHONE,
                Manifest.permission.ACCESS_NETWORK_STATE,
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.WRITE_CALL_LOG,
                Manifest.permission.READ_CALL_LOG
        }, 101);
        initView();
        int size = getSharedPreferences("zk2", MODE_PRIVATE).getAll().size();
        if(size>0){//已经登录过了
            Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
        }
    }

    public void login(View view) {
        if(etUsername.getText().toString().isEmpty()||etPassword.getText().toString().isEmpty()){
            Toast.makeText(this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
        }else{
            getSharedPreferences("zk2", MODE_PRIVATE).edit()
                    .putString("username", etUsername.getText().toString())
                    .putString("password",etPassword.getText().toString())
                    .commit();//保存登录状态
            Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
        }
        
    }

    private void initView() {
        etUsername = (EditText) findViewById(R.id.et_username);
        etPassword = (EditText) findViewById(R.id.et_password);
    }
}